
const {
    addaumbles,
    getoneaumble,
    subfilename,
    editaumbleinfo,
    getaumbleinfo,
    getaumblecount,
    addaumblepic,
    getaumblepic,
    getaumblepicnum,
    deletePicserve,
    delectaumble
} = require("../serve/Photo.js")
const sequelize = require("../model/db.js")
let { deleteimg } = require("../util/qiniuUpload.js")
const Response = require("../util/respanse.js")
/* 创建相册 */
const addaumble = async (req, res) => {
    try {
        let { album_name, description } = req.body
        let result = await addaumbles(album_name, description)
        console.log(result.msg);
        if (result.code) {
            Response.successresponse(res, {
                code: 200,
                message: "添加相册成功",
                data: result.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                message: "添加相册失败",
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse({
            code: 500,
            message: "服务器错误",
            err: error
        })
    }
}
/* 提交文件的名字 */
const submitFileName = async (req, res) => {
    try {
        let { aumble_id, file_name } = req.body
        aumble_id = (typeof aumble_id == "string") ? parseInt(aumble_id) : aumble_id
        let result = await getoneaumble(aumble_id)
        if (result.code) {
            let ress = await subfilename(aumble_id, file_name)
            if (ress.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "提交文件名成功",
                    data: ress.data
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    message: "提交文件名失败"
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                message: "相册不存在"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse({
            code: 500,
            msg: "服务器错误",
            err: error
        })
    }
}
/* 编辑相册信息 */
const editaumble = async (req, res) => {
    try {
        let { id, name, description, album_cover } = req.body
        id = (typeof id == "string") ? parseInt(id) : id
        let result = await getoneaumble(id)
        if (result.code) {
            if (!album_cover) {
                let ok = await editaumbleinfo(id, name, description)
                if (ok.code) {
                    Response.successresponse(res, {
                        code: 200,
                        message: "编辑相册成功"
                    })
                } else {
                    Response.errorresponse(res, {
                        code: 400,
                        msg: "编辑相册失败"
                    })
                }
            } else {
                /* 先删除七牛云图片 */
                deleteimg(result.data.album_cover.split("/").pop(), async function (err, respBody, respInfo) {
                    if (err) {
                        console.log(err);
                        //throw err;
                    } else {
                        if (respInfo.statusCode == 200) {
                            let ok = await editaumbleinfo(id, name, description, album_cover)
                            if (ok.code) {
                                Response.successresponse(res, {
                                    code: 200,
                                    message: "编辑相册成功"
                                })
                            }
                        } else {
                            Response.errorresponse(res, {
                                code: 400,
                                msg: "编辑相册失败"
                            })
                        }
                    }
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "相册不存在"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "服务器错误"
        })
    }
}
/* 分页获取相册 */
const getaumble = async (req, res) => {
    try {
        let { page, pageSize } = req.body
        let ress = await getaumbleinfo(page, pageSize)
        console.log(ress.msg);
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                msg: "获取相册成功",
                data: {
                    data: ress.data,
                }
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: ress.msg
            })
        }

    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "服务器错误",
            err: error
        })
    }
}
/* 获取数量 */
const getcount = async (req, res) => {
    try {
        let count = await getaumblecount()
        if (count) {
            Response.successresponse(res, {
                code: 200,
                message: "获取相册数量成功",
                data: {
                    count
                }
            })
        } else {
            Response.successresponse(res, {
                code: 400,
                message: "暂无数据",
                data: {
                    count
                }
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            err: error,
            msg: "服务器错误"
        })
    }
}
/* 添加图片 */
const addPic = async (req, res) => {
    try {
        let { aumble_id, file_namelist } = req.body
        let ress = await getoneaumble(aumble_id)
        console.log(ress.msg);
        if (ress.code) {
            let rees = await addaumblepic(aumble_id, file_namelist)
            if (rees.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "添加图片成功",
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: rees.msg
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: ress.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "服务器错误",
            err: error
        })
    }
}
/* 获取图片 */
const getPic = async (req, res) => {
    try {
        let { aumble_id, page, pageSize } = req.body
        let ress = await getoneaumble(aumble_id)
        if (ress.code) {
            let rees = await getaumblepic(aumble_id, page, pageSize)
            console.log(rees.msg);
            if (rees.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "获取图片成功",
                    data: rees.data
                })
            }else{
                Response.successresponse(res, {
                    code: 400,
                    message: rees.msg,
                    data: rees.data
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: ress.msg
            })
        }
    } catch (error) {

    }
}
/* 图片数量 */
const alltotaldata = async (req, res) => {
    try {
        let { album_id } = req.query
        let ress = await getoneaumble(album_id)
        if (ress.code) {
            let rees = await getaumblepicnum(album_id)
            console.log(rees,"dasda");
            if (rees.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "获取图片成功",
                    data: rees.data
                })
            } else {
                Response.successresponse(res, {
                    code: 400,
                    message: "暂无数据",
                    data: rees.data
                })
            }
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res,{
            code:500,
            err:error,
            msg:"服务器错误"
        })
    }
}
/* 批量删除 */
const deletePic = async (req,res)=>{
    let {todelectIdlist,album_id} =req.body
    try {
        let ress = await getoneaumble(album_id)
        if(ress.code){
            let rees = await deletePicserve(album_id,todelectIdlist)
            if(rees.code){
                Response.successresponse(res,{
                    code:200,
                    message:"删除成功"
                })
            }else{
                Response.errorresponse(res,{
                    code:400,
                    msg:"删除失败"
                })
            }
        }else{
            Response.errorresponse(res, {
                code: 400,
                msg: ress.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res,{
            code:500,
            msg:"出错了",
            err: error
        })
    }
}
/* 删除相册 */
const delectaumbles = async (req,res)=>{
    try {
        let {aumble_id} = req.body
        let ress = await getoneaumble(aumble_id)
        if (ress.code) {
            let rees = await delectaumble(aumble_id,ress.data.album_cover)
            if (rees.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "删除成功"
                })
            }else{
                Response.errorresponse(res, {
                    code: 400,
                    msg: "删除失败"
                })
            }
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res,{
            code:500,
            msg:"出错了",
            err: error
        })
    }
}
module.exports = {
    delectaumbles,
    addaumble,
    submitFileName,
    editaumble,
    getaumble,
    getcount,
    addPic,
    getPic,
    deletePic,
    alltotaldata
}